package com.sinosoft.comm.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author 作者:chenjie
 * @version 创建时间：2011-3-3 下午10:27:58 
 * 类说明:
 */
@Service
@Lazy(false)
public class SpringContext implements ApplicationContextAware {

	private static ApplicationContext applicationContext;

	@Override
    public void setApplicationContext(ApplicationContext ac)
			throws BeansException {
		applicationContext = ac;
	}

	public static ApplicationContext getApplicationContext() {
		return applicationContext;
	}

	/*public static Object getBean(String name) {
		return applicationContext.getBean(name);
	}*/
	public static <T> T getBean(Class<T> requiredType) {
		return applicationContext.getBean(requiredType);
	}
	@SuppressWarnings("unchecked")
	public static <T> T getBean(String name) {
		return (T) applicationContext.getBean(name);
	}
	public static  <S> S getServiceBeansByClass(Class<S> c){
		Map<String, S> map = applicationContext.getBeansOfType(c);
		for (Map.Entry<String, S> entry : map.entrySet()) {
			if(entry.getKey().toLowerCase().indexOf(c.getSimpleName().toLowerCase())!=-1) {
				return entry.getValue();
			}
		}
		return null;
	}
}